package com.itheima;

import java.util.ArrayList;

class User{
    private String name;
    private int age;
    private String sex;

    public User() {
    }

    public User(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void show(){
        System.out.println(this.name + "  " + age+ "  " + sex);
    }
}


public class Test03 {
    public static void main(String[] args) {
        //1. 定义5个User对象,并存入ArrayList集合
        User user1 = new User("张三",18,"男");
        User user2 = new User("李四",17,"男");
        User user3 = new User("王五",19,"女");
        User user4 = new User("赵六",20,"男");
        User user5 = new User("王八",8,"男");
        ArrayList<User> list = new ArrayList();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        //调用getUser方法, 筛选出符合条件的数据
        ArrayList<User> newList = getUser(list);
        //遍历结果
        for (int i = 0; i < newList.size(); i++) {
            User user = newList.get(i);
            user.show();
        }
    }

    /*
        功能: 将传过来的集合中年龄大于18岁并且性别为"男"的User对象筛选出来,存入新集合,并将新集合返回
     */
    public static ArrayList<User> getUser(ArrayList<User> users){
        //1. 定义一个新容器,用来存放筛选后符合条件的User对象
        ArrayList<User> newList = new ArrayList<>();
        //2. 遍历老容器,筛选出年龄大于等于18岁并且性别为"男"的User对象
        for (int i = 0; i < users.size(); i++) {
            //2.1 拿到老容器中每一个User对象
            User user = users.get(i);
            //2.2 判断当前索引位置的User对象是否符合条件
            if (user.getAge() >= 18 && user.getSex().equals("男")){
                //2.3 将符合条件的User对象存入新容器
                newList.add(user);
            }
        }
        //3. 将新集合容器进行返回
        return newList;
    }
}
